from typing import Any

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: str | None = None

class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: str | None = None

@app.post("/user/", response_model=UserOut,response_model_exclude_unset=True) #定义输出的模型样式，会自动过滤没有的,response_model_exclude_unset表示过滤掉默认值，只要实际设置的值
async def create_user(user: UserIn) -> Any:
    return user




class Item(BaseModel):
    name: str
    description: str| None = None
    price: float
    tax: float = 10.5
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The Bar fighters", "price": 62, "tax": 20.2},
    "baz": {
        "name": "Baz",
        "description": "There goes my baz",
        "price": 50.2,
        "tax": 10.5,
    },
}

@app.get(
    "/items/{item_id}/name",
    response_model=Item,
    response_model_include={"name", "description"}, #只要哪些属性，你也可以用list，fastapi自己给你转为set
)


async def read_item_name(item_id: str):
    return items[item_id]


@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude={"tax"}) #排除哪些属性
async def read_item_public_data(item_id: str):
    return items[item_id]